﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace ImageTagEditor.ValueConverters
{
	/// <summary>
	/// Converts float?'s to string and back
	/// </summary>
	public class NullableFloatValueConverter : IValueConverter
	{

		/// <summary>
		/// Convert from a float? to a string
		/// </summary>
		/// <param name="value">The float? to convert</param>
		/// <param name="targetType">Not Used</param>
		/// <param name="parameter">Not Used</param>
		/// <param name="culture">Not Used</param>
		/// <returns>The string representation of the float?</returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (value == null)
				return "";
			if (value is float?)
			{
				float? nFloat = value as float?;
				return nFloat.ToString();
			}
			throw new Exception("NullableFloatValueConverter can only convert objects of type float?");
		}

		/// <summary>
		/// Convert from a string to a float?
		/// </summary>
		/// <param name="value">The string to convert back from</param>
		/// <param name="targetType">Not Used</param>
		/// <param name="parameter">Not Used</param>
		/// <param name="culture">Not Used</param>
		/// <returns>The float? converted back from the string</returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (value is string)
			{
				string stringValue = value as string;
				if (String.IsNullOrEmpty(stringValue))
					return null;
				else
					return float.Parse(stringValue);
			}
			throw new Exception("NullableFloatValueConverter can only convert back from objects of type string");
		}

	}
}
